package cn.basic.social.api.dingtalk.channel.internal.request;

import cn.basic.social.api.CommonConstants;
import cn.basic.social.api.dingtalk.BaseDingRequest;
import cn.basic.social.api.dingtalk.channel.internal.response.InternalMessageAsyncSendV2Response;
import cn.basic.social.api.exception.ApiRuleException;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.annotation.JSONField;
import com.google.gson.annotations.SerializedName;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName InternalMessageAsyncSendV2Request
 * @Description 发送工作通知请求
 * https://open.dingtalk.com/document/orgapp/asynchronous-sending-of-enterprise-session-messages
 * 1、企业内部应用发送消息单次最多只能给5000人发送，第三方企业应用发送消息单次最多能给1000人发送。
 * 2、给同一员工一天只能发送一条内容相同的消息通知。
 * 3、企业内部应用每天给每个员工最多可发送500条消息通知，第三方企业应用最多可发送100条。
 * 4、企业内部应用或第三方企业应用发送消息时，每分钟最多有5000人可以接收到消息。
 * @Author yk.zlf
 * @Date 2023/10/20 16:41
 */
public class InternalMessageAsyncSendV2Request extends BaseDingRequest<InternalMessageAsyncSendV2Response> implements Serializable {

    private final String requestUrl = "https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2";
    private String topHttpMethod = CommonConstants.HTTP_METHOD_POST;
    private String topContentType = "application/json";

    /**
     * 微应用的id
     */
    @SerializedName(value = "agent_id")
    @JSONField(name = "agent_id")
    private Long agentId;

    /**
     * 接收者的部门id列表
     */
    @SerializedName(value = "dept_id_list")
    @JSONField(name = "dept_id_list")
    private String deptIdList;

    /**
     * 消息体，具体见文档
     */
    private Msg msg;

    /**
     * 是否发送给企业全部用户
     */
    @SerializedName(value = "to_all_user")
    @JSONField(name = "to_all_user")
    private Boolean toAllUser;

    /**
     * 接收者的用户userid列表
     */
    @SerializedName(value = "userid_list")
    @JSONField(name = "userid_list")
    private String useridList;

    public InternalMessageAsyncSendV2Request() {
    }

    public InternalMessageAsyncSendV2Request(Long agentId, String deptIdList, Msg msg, Boolean toAllUser, String useridList) {
        this.agentId = agentId;
        this.deptIdList = deptIdList;
        this.msg = msg;
        this.toAllUser = toAllUser;
        this.useridList = useridList;
    }

    public Long getAgentId() {
        return agentId;
    }

    public void setAgentId(Long agentId) {
        this.agentId = agentId;
    }

    public String getDeptIdList() {
        return deptIdList;
    }

    public void setDeptIdList(String deptIdList) {
        this.deptIdList = deptIdList;
    }

    public Msg getMsg() {
        return msg;
    }

    public void setMsg(Msg msg) {
        this.msg = msg;
    }

    public Boolean getToAllUser() {
        return toAllUser;
    }

    public void setToAllUser(Boolean toAllUser) {
        this.toAllUser = toAllUser;
    }

    public String getUseridList() {
        return useridList;
    }

    public void setUseridList(String useridList) {
        this.useridList = useridList;
    }

    @Override
    public String getTopHttpMethod() {
        return this.topHttpMethod;
    }

    @Override
    public void setTopHttpMethod(String topHttpMethod) {
        this.topHttpMethod = topHttpMethod;
    }

    @Override
    public Map<String, Object> getTextParams() {
        Map<String, Object> txtParams = new HashMap<>();
        txtParams.put("agent_id", this.agentId);
        txtParams.put("dept_id_list", this.deptIdList);
        txtParams.put("msg", this.msg);
        txtParams.put("to_all_user", this.toAllUser);
        txtParams.put("userid_list", this.useridList);
        return txtParams;
    }

    @Override
    public Class<InternalMessageAsyncSendV2Response> getResponseClass() {
        return InternalMessageAsyncSendV2Response.class;
    }

    @Override
    public void check() throws ApiRuleException {
        if (null == this.agentId || this.agentId < 0) {
            throw new ApiRuleException("-1", "发送工作通知调用失败，client-error:Missing required arguments: agentId");
        }
        if (this.useridList != null) {
            String[] list = this.useridList.split(",");
            if (list != null && list.length > 500) {
                throw new ApiRuleException("-1", "发送工作通知调用失败，client-error:Invalid arguments:the array size of useridList must be less than 500.");
            }
        }
        if (this.deptIdList != null) {
            String[] list = this.deptIdList.split(",");
            if (list != null && list.length > 500) {
                throw new ApiRuleException("-1", "发送工作通知调用失败，client-error:Invalid arguments:the array size of deptIdList must be less than 500.");
            }
        }
    }

    @Override
    public String getTopContentType() {
        return this.topContentType;
    }

    @Override
    public void setTopContentType(String topContentType) {
        this.topContentType = topContentType;
    }

    @Override
    public String getRequestUrl() {
        return this.requestUrl;
    }

    /**
     * 文本消息
     *
     * @author top auto create
     * @since 1.0, null
     */
    public static class Text implements Serializable {
        private static final long serialVersionUID = 7136822967334988419L;
        /**
         * 文本消息
         */
        private String content;

        public String getContent() {
            return this.content;
        }
        public void setContent(String content) {
            this.content = content;
        }
    }

    /**
     * 图片消息
     *
     * @author top auto create
     * @since 1.0, null
     */
    public static class Image implements Serializable {
        private static final long serialVersionUID = 5111316984979551457L;
        /**
         * 图片消息
         */
        @SerializedName(value = "media_id")
        @JSONField(name = "media_id")
        private String mediaId;

        public String getMediaId() {
            return this.mediaId;
        }
        public void setMediaId(String mediaId) {
            this.mediaId = mediaId;
        }
    }

    /**
     * 链接消息
     *
     * @author top auto create
     * @since 1.0, null
     */
    public static class Link implements Serializable {
        private static final long serialVersionUID = 2757883691527167563L;
        /**
         * 消息点击链接地址
         */
        private String messageUrl;
        /**
         * 图片地址
         */
        private String picUrl;
        /**
         * 消息标题，建议100字符以内
         */
        private String text;
        /**
         * 消息描述，建议500字符以内
         */
        private String title;

        public String getMessageUrl() {
            return this.messageUrl;
        }
        public void setMessageUrl(String messageUrl) {
            this.messageUrl = messageUrl;
        }
        public String getPicUrl() {
            return this.picUrl;
        }
        public void setPicUrl(String picUrl) {
            this.picUrl = picUrl;
        }
        public String getText() {
            return this.text;
        }
        public void setText(String text) {
            this.text = text;
        }
        public String getTitle() {
            return this.title;
        }
        public void setTitle(String title) {
            this.title = title;
        }
    }

    /**
     * 文件消息
     *
     * @author top auto create
     * @since 1.0, null
     */
    public static class File implements Serializable {
        private static final long serialVersionUID = 8133117921592134818L;
        /**
         * 媒体文件id。引用的媒体文件最大10MB
         */
        @SerializedName(value = "media_id")
        @JSONField(name = "media_id")
        private String mediaId;

        public String getMediaId() {
            return this.mediaId;
        }
        public void setMediaId(String mediaId) {
            this.mediaId = mediaId;
        }
    }

    /**
     * 语音消息
     *
     * @author top auto create
     * @since 1.0, null
     */
    public static class Voice implements Serializable {
        private static final long serialVersionUID = 8116843149852161251L;
        /**
         * 正整数，小于60，表示音频时长
         */
        private String duration;
        /**
         * 媒体文件id。2MB，播放长度不超过60s，AMR格式
         */
        @SerializedName(value = "media_id")
        @JSONField(name = "media_id")
        private String mediaId;

        public String getDuration() {
            return this.duration;
        }
        public void setDuration(String duration) {
            this.duration = duration;
        }
        public String getMediaId() {
            return this.mediaId;
        }
        public void setMediaId(String mediaId) {
            this.mediaId = mediaId;
        }
    }

    /**
     * 单行富文本信息
     *
     * @author top auto create
     * @since 1.0, null
     */
    public static class Rich implements Serializable {
        private static final long serialVersionUID = 4322925993384868721L;
        /**
         * 单行富文本信息的数目
         */
        private String num;
        /**
         * 单行富文本信息的单位
         */
        private String unit;

        public String getNum() {
            return this.num;
        }
        public void setNum(String num) {
            this.num = num;
        }
        public String getUnit() {
            return this.unit;
        }
        public void setUnit(String unit) {
            this.unit = unit;
        }
    }

    /**
     * 消息体的表单，最多显示6个，超过会被隐藏
     *
     * @author top auto create
     * @since 1.0, null
     */
    public static class Form implements Serializable {
        /**
         * 消息体的关键字
         */
        private String key;
        /**
         * 消息体的关键字对应的值
         */
        private String value;

        public String getKey() {
            return this.key;
        }
        public void setKey(String key) {
            this.key = key;
        }
        public String getValue() {
            return this.value;
        }
        public void setValue(String value) {
            this.value = value;
        }
    }

    /**
     * 消息体
     *
     * @author top auto create
     * @since 1.0, null
     */
    public static class Body implements Serializable {
        private static final long serialVersionUID = 7236545756276272138L;
        /**
         * 自定义的作者名字
         */
        private String author;
        /**
         * 消息体的内容，最多显示3行
         */
        private String content;
        /**
         * 自定义的附件数目。此数字仅供显示，钉钉不作验证
         */
        @SerializedName(value = "file_count")
        @JSONField(name = "file_count")
        private String fileCount;
        /**
         * 消息体的表单，最多显示6个，超过会被隐藏
         */
        private List<InternalMessageAsyncSendV2Request.Form> form;
        /**
         * 消息体中的图片，支持图片资源@mediaId
         */
        private String image;
        /**
         * 单行富文本信息
         */
        private InternalMessageAsyncSendV2Request.Rich rich;
        /**
         * 消息体的标题，建议50个字符以内
         */
        private String title;

        public String getAuthor() {
            return this.author;
        }
        public void setAuthor(String author) {
            this.author = author;
        }
        public String getContent() {
            return this.content;
        }
        public void setContent(String content) {
            this.content = content;
        }
        public String getFileCount() {
            return this.fileCount;
        }
        public void setFileCount(String fileCount) {
            this.fileCount = fileCount;
        }
        public List<InternalMessageAsyncSendV2Request.Form> getForm() {
            return this.form;
        }
        public void setForm(List<InternalMessageAsyncSendV2Request.Form> form) {
            this.form = form;
        }
        public String getImage() {
            return this.image;
        }
        public void setImage(String image) {
            this.image = image;
        }
        public InternalMessageAsyncSendV2Request.Rich getRich() {
            return this.rich;
        }
        public void setRich(InternalMessageAsyncSendV2Request.Rich rich) {
            this.rich = rich;
        }
        public String getTitle() {
            return this.title;
        }
        public void setTitle(String title) {
            this.title = title;
        }
    }

    /**
     * 消息头部内容
     *
     * @author top auto create
     * @since 1.0, null
     */
    public static class Head implements Serializable {
        private static final long serialVersionUID = 4122521384969164574L;
        /**
         * 消息头部的背景颜色。长度限制为8个英文字符，其中前2为表示透明度，后6位表示颜色值。不要添加0x
         */
        private String bgcolor;
        /**
         * 消息的头部标题 (向普通会话发送时有效，向企业会话发送时会被替换为微应用的名字)，长度限制为最多10个字符
         */
        private String text;

        public String getBgcolor() {
            return this.bgcolor;
        }
        public void setBgcolor(String bgcolor) {
            this.bgcolor = bgcolor;
        }
        public String getText() {
            return this.text;
        }
        public void setText(String text) {
            this.text = text;
        }
    }

    /**
     * 状态栏
     *
     * @author top auto create
     * @since 1.0, null
     */
    public static class StatusBar implements Serializable {
        private static final long serialVersionUID = 3557585852256188719L;
        /**
         * 状态栏背景色，默认为黑色
         */
        @SerializedName(value = "status_bg")
        @JSONField(name = "status_bg")
        private String statusBg;
        /**
         * 状态栏文案
         */
        @SerializedName(value = "status_value")
        @JSONField(name = "status_value")
        private String statusValue;

        public String getStatusBg() {
            return this.statusBg;
        }
        public void setStatusBg(String statusBg) {
            this.statusBg = statusBg;
        }
        public String getStatusValue() {
            return this.statusValue;
        }
        public void setStatusValue(String statusValue) {
            this.statusValue = statusValue;
        }
    }

    /**
     * OA消息
     *
     * @author top auto create
     * @since 1.0, null
     */
    public static class OA implements Serializable {
        private static final long serialVersionUID = 2599774517599455299L;
        /**
         * 消息体
         */
        private InternalMessageAsyncSendV2Request.Body body;
        /**
         * 消息头部内容
         */
        private InternalMessageAsyncSendV2Request.Head head;
        /**
         * 消息点击链接地址，当发送消息为小程序时支持小程序跳转链接
         */
        @SerializedName(value = "message_url")
        @JSONField(name = "message_url")
        private String messageUrl;
        /**
         * PC端点击消息时跳转到的地址
         */
        @SerializedName(value = "pc_message_url")
        @JSONField(name = "pc_message_url")
        private String pcMessageUrl;
        /**
         * 状态栏
         */
        @SerializedName(value = "status_bar")
        @JSONField(name = "status_bar")
        private InternalMessageAsyncSendV2Request.StatusBar statusBar;

        public InternalMessageAsyncSendV2Request.Body getBody() {
            return this.body;
        }
        public void setBody(InternalMessageAsyncSendV2Request.Body body) {
            this.body = body;
        }
        public InternalMessageAsyncSendV2Request.Head getHead() {
            return this.head;
        }
        public void setHead(InternalMessageAsyncSendV2Request.Head head) {
            this.head = head;
        }
        public String getMessageUrl() {
            return this.messageUrl;
        }
        public void setMessageUrl(String messageUrl) {
            this.messageUrl = messageUrl;
        }
        public String getPcMessageUrl() {
            return this.pcMessageUrl;
        }
        public void setPcMessageUrl(String pcMessageUrl) {
            this.pcMessageUrl = pcMessageUrl;
        }
        public InternalMessageAsyncSendV2Request.StatusBar getStatusBar() {
            return this.statusBar;
        }
        public void setStatusBar(InternalMessageAsyncSendV2Request.StatusBar statusBar) {
            this.statusBar = statusBar;
        }
    }

    /**
     * markdown消息
     *
     * @author top auto create
     * @since 1.0, null
     */
    public static class Markdown implements Serializable {
        private static final long serialVersionUID = 4352594231334542361L;
        /**
         * markdown格式的消息，建议500字符以内
         */
        private String text;
        /**
         * 首屏会话透出的展示内容
         */
        private String title;

        public String getText() {
            return this.text;
        }
        public void setText(String text) {
            this.text = text;
        }
        public String getTitle() {
            return this.title;
        }
        public void setTitle(String title) {
            this.title = title;
        }
    }

    /**
     * 使用独立跳转ActionCard样式时的按钮列表；必须与btn_orientation同时设置
     *
     * @author top auto create
     * @since 1.0, null
     */
    public static class BtnJsonList implements Serializable {
        private static final long serialVersionUID = 1441754498859884793L;
        /**
         * 消息点击链接地址，当发送消息为小程序时支持小程序跳转链接，最长500个字符
         */
        @SerializedName(value = "action_url")
        @JSONField(name = "action_url")
        private String actionUrl;
        /**
         * 使用独立跳转ActionCard样式时的按钮的标题，最长20个字符
         */
        private String title;

        public String getActionUrl() {
            return this.actionUrl;
        }
        public void setActionUrl(String actionUrl) {
            this.actionUrl = actionUrl;
        }
        public String getTitle() {
            return this.title;
        }
        public void setTitle(String title) {
            this.title = title;
        }
    }

    /**
     * 卡片消息
     *
     * @author top auto create
     * @since 1.0, null
     */
    public static class ActionCard implements Serializable {
        private static final long serialVersionUID = 2194296449645234598L;
        /**
         * 使用独立跳转ActionCard样式时的按钮列表；必须与btn_orientation同时设置
         */
        @SerializedName(value = "btn_json_list")
        @JSONField(name = "btn_json_list")
        private List<InternalMessageAsyncSendV2Request.BtnJsonList> btnJsonList;
        /**
         * 使用独立跳转ActionCard样式时的按钮排列方式，竖直排列(0)，横向排列(1)；必须与btn_json_list同时设置
         */
        @SerializedName(value = "btn_orientation")
        @JSONField(name = "btn_orientation")
        private String btnOrientation;
        /**
         * 消息内容，支持markdown，语法参考标准markdown语法。建议1000个字符以内
         */
        private String markdown;
        /**
         * 使用整体跳转ActionCard样式时的标题，必须与single_url同时设置，最长20个字符
         */
        @SerializedName(value = "single_title")
        @JSONField(name = "single_title")
        private String singleTitle;
        /**
         * 消息点击链接地址，当发送消息为小程序时支持小程序跳转链接，最长500个字符
         */
        @SerializedName(value = "single_url")
        @JSONField(name = "single_url")
        private String singleUrl;
        /**
         * 透出到会话列表和通知的文案，最长64个字符
         */
        private String title;

        public List<InternalMessageAsyncSendV2Request.BtnJsonList> getBtnJsonList() {
            return this.btnJsonList;
        }
        public void setBtnJsonList(List<InternalMessageAsyncSendV2Request.BtnJsonList> btnJsonList) {
            this.btnJsonList = btnJsonList;
        }
        public String getBtnOrientation() {
            return this.btnOrientation;
        }
        public void setBtnOrientation(String btnOrientation) {
            this.btnOrientation = btnOrientation;
        }
        public String getMarkdown() {
            return this.markdown;
        }
        public void setMarkdown(String markdown) {
            this.markdown = markdown;
        }
        public String getSingleTitle() {
            return this.singleTitle;
        }
        public void setSingleTitle(String singleTitle) {
            this.singleTitle = singleTitle;
        }
        public String getSingleUrl() {
            return this.singleUrl;
        }
        public void setSingleUrl(String singleUrl) {
            this.singleUrl = singleUrl;
        }
        public String getTitle() {
            return this.title;
        }
        public void setTitle(String title) {
            this.title = title;
        }
    }

    /**
     * 消息体，具体见文档
     *
     * @author top auto create
     * @since 1.0, null
     */
    public static class Msg implements Serializable {
        private static final long serialVersionUID = 8367182425825846876L;
        /**
         * 卡片消息
         */
        @SerializedName(value = "action_card")
        @JSONField(name = "action_card")
        private InternalMessageAsyncSendV2Request.ActionCard actionCard;
        /**
         * 文件消息
         */
        private InternalMessageAsyncSendV2Request.File file;
        /**
         * 图片消息
         */
        private InternalMessageAsyncSendV2Request.Image image;
        /**
         * 链接消息
         */
        private InternalMessageAsyncSendV2Request.Link link;
        /**
         * markdown消息
         */
        private InternalMessageAsyncSendV2Request.Markdown markdown;
        /**
         * 消息类型
         */
        private String msgtype;
        /**
         * OA消息
         */
        private InternalMessageAsyncSendV2Request.OA oa;
        /**
         * 文本消息
         */
        private InternalMessageAsyncSendV2Request.Text text;
        /**
         * 语音消息
         */
        private InternalMessageAsyncSendV2Request.Voice voice;

        public InternalMessageAsyncSendV2Request.ActionCard getActionCard() {
            return this.actionCard;
        }
        public void setActionCard(InternalMessageAsyncSendV2Request.ActionCard actionCard) {
            this.actionCard = actionCard;
        }
        public InternalMessageAsyncSendV2Request.File getFile() {
            return this.file;
        }
        public void setFile(InternalMessageAsyncSendV2Request.File file) {
            this.file = file;
        }
        public InternalMessageAsyncSendV2Request.Image getImage() {
            return this.image;
        }
        public void setImage(InternalMessageAsyncSendV2Request.Image image) {
            this.image = image;
        }
        public InternalMessageAsyncSendV2Request.Link getLink() {
            return this.link;
        }
        public void setLink(InternalMessageAsyncSendV2Request.Link link) {
            this.link = link;
        }
        public InternalMessageAsyncSendV2Request.Markdown getMarkdown() {
            return this.markdown;
        }
        public void setMarkdown(InternalMessageAsyncSendV2Request.Markdown markdown) {
            this.markdown = markdown;
        }
        public String getMsgtype() {
            return this.msgtype;
        }
        public void setMsgtype(String msgtype) {
            this.msgtype = msgtype;
        }
        public InternalMessageAsyncSendV2Request.OA getOa() {
            return this.oa;
        }
        public void setOa(InternalMessageAsyncSendV2Request.OA oa) {
            this.oa = oa;
        }
        public InternalMessageAsyncSendV2Request.Text getText() {
            return this.text;
        }
        public void setText(InternalMessageAsyncSendV2Request.Text text) {
            this.text = text;
        }
        public InternalMessageAsyncSendV2Request.Voice getVoice() {
            return this.voice;
        }
        public void setVoice(InternalMessageAsyncSendV2Request.Voice voice) {
            this.voice = voice;
        }
    }
}
